package andev.axplorer.Menu;

import android.graphics.Color;
import android.graphics.Point;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import andev.aandroidlib.activity.AActivity;
import andev.axplorer.MainActivity;
import andev.axplorer.R;
import andev.axplorer.Tiles.TilesLayout;
import andev.axplorer.Tiles.TilesLayoutStorage;

/**
 * Created by Angus on 2015/8/2.
 */
public class RightMenu {
    private static PopupWindow popupWindow;

    public static void showRightMenu(View parent, int x, int y) {
        AActivity activity = (AActivity) parent.getContext();

        LinearLayout menuLayout = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.checkedtile_right_menu, null);
        InitComp(menuLayout);

        popupWindow = new PopupWindow(menuLayout);

        Point screenSize = activity.getScreenSize();
        popupWindow.setWidth(screenSize.x / 2);
        popupWindow.setHeight(screenSize.y / 10 * 4);

        if (parent instanceof GridView)
            popupWindow.showAtLocation(parent, Gravity.TOP | Gravity.START, x, y);
        else popupWindow.showAsDropDown(parent);
    }

    private static void InitComp(LinearLayout menuLayout) {
        TextView tv_open, tv_refresh, tv_copy, tv_cut, tv_delete, tv_rename, tv_paste;
        tv_open = (TextView) menuLayout.findViewById(R.id.tv_open);
        tv_refresh = (TextView) menuLayout.findViewById(R.id.tv_refresh);
        tv_copy = (TextView) menuLayout.findViewById(R.id.tv_copy);
        tv_cut = (TextView) menuLayout.findViewById(R.id.tv_cut);
        tv_delete = (TextView) menuLayout.findViewById(R.id.tv_delete);
        tv_rename = (TextView) menuLayout.findViewById(R.id.tv_rename);
        tv_paste = (TextView) menuLayout.findViewById(R.id.tv_paste);

        View.OnClickListener c = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_open:
                        if (TilesLayout.checkedTile != null)
                            TilesLayout.checkedTile.goInDir();
                        else if (TilesLayoutStorage.checkedStorage != null)
                            TilesLayoutStorage.checkedStorage.goInDir();
                        break;
                    case R.id.tv_refresh:
                        ((MainActivity) v.getContext()).refresh();
                        break;
                    case R.id.tv_copy:
                        break;
                    case R.id.tv_cut:
                        break;
                    case R.id.tv_delete:
                        ((MainActivity) v.getContext()).deleteFile();
                        break;
                    case R.id.tv_rename:
                        break;
                    case R.id.tv_paste:
                        break;
                }
                cancelRightMenu();
            }
        };

        tv_open.setOnClickListener(c);
        tv_refresh.setOnClickListener(c);
        tv_copy.setOnClickListener(c);
        tv_cut.setOnClickListener(c);
        tv_delete.setOnClickListener(c);
        tv_rename.setOnClickListener(c);
        tv_paste.setOnClickListener(c);

        if (TilesLayout.checkedTile == null && TilesLayoutStorage.checkedStorage == null) {
            tv_open.setClickable(false);
            tv_open.setTextColor(Color.parseColor("#DCDCDC"));
        }

    }

    public static void cancelRightMenu() {
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }
}
